﻿using OvenParameterForm.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OvenParameterForm.Technics
{
    public class 抽充 : TechnicsBaseClass
    {
        public string 抽充次数 { get; set; }
        public string 抽气设定 { get; set; }
        public string 充气设定 { get; set; }
        private int 当前次数 = 0;
        private double 当前压力 = 0.0;
        private bool 正在充气 = false;
        private bool 正在抽气 = false;
        private bool 充气达标 = false;
        private bool 抽气达标 = false;
        private double 烘箱真空计读数 = 0.0;
        public 抽充(string name) : base(name)
        {
        }
        public 抽充(string[] strings) : base(strings)
        {
            抽充次数 = strings[1];
            抽气设定 = strings[2];
            充气设定 = strings[3];
        }

        public override void Run()
        {
            PLC.WriteData(ovenPLCaddress.烘箱保温地址, "0");
            PLC.WriteData(ovenPLCaddress.充气地址, "0");
            PLC.WriteData(ovenPLCaddress.抽气地址, "0");
            当前次数 = 0;
            base.Run();
        }

        protected override void updatetime(object state)
        {
            当前压力 = Convert.ToDouble(PLC.ReadData(ovenPLCaddress.烘箱压力地址));
            烘箱真空计读数 = Convert.ToDouble(PLC.InovanceAM400PLCRead(ovenPLCaddress.真空计整数地址,16) + "." + PLC.InovanceAM400PLCRead(ovenPLCaddress.真空计小数地址,16)) * Math.Pow(10, Convert.ToInt16(PLC.InovanceAM400PLCRead(ovenPLCaddress.真空计指数地址,16)));
            正在充气 = PLC.ReadData(ovenPLCaddress.充气地址).Equals("1");
            正在抽气 = PLC.ReadData(ovenPLCaddress.抽气地址).Equals("1");
            充气达标 = 当前压力 / 1000 >= Convert.ToDouble(充气设定);
            抽气达标 = 烘箱真空计读数 <= Convert.ToDouble(抽气设定);
            if (当前次数 < int.Parse(抽充次数))
            {
                switch (PublicFunc.BoolArrayGetValue(new bool[2] { 正在充气, 正在抽气}))
                {
                    case 0:
                        PLC.WriteData(ovenPLCaddress.抽气地址, "1");
                        sendStatus($"开始抽气....次数：{当前次数}/{抽充次数}");
                        //开始抽气
                        break;
                    case 1:
                        if (抽气达标)
                        {
                            PLC.WriteData(ovenPLCaddress.抽气地址, "0");
                            PLC.WriteData(ovenPLCaddress.充气地址, "1");
                            sendStatus($"开始充气....次数：{当前次数}/{抽充次数}");
                        }
                        else
                        {
                            sendStatus($"正在抽气....次数：{当前次数}/{抽充次数}");
                            //正在抽气
                        }
                        break;
                    case 2:
                        if (充气达标)
                        {
                            PLC.WriteData(ovenPLCaddress.充气地址, "0");
                            当前次数++;
                        }
                        else
                        {
                            sendStatus($"正在充气....次数：{当前次数}/{抽充次数}");
                            //正在充气
                        }
                        break;
                }
            }
        }

        protected override bool Complete()
        {
            return 当前次数 >= int.Parse(抽充次数);
        }

        protected override void ClearVariable()
        {
            PLC.WriteData(ovenPLCaddress.抽气地址, "0");
            PLC.WriteData(ovenPLCaddress.充气地址, "0");
        }
    }
}
